package com.huitong.admin.system.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.huitong.admin.system.domain.dto.StudentDTO;
import com.huitong.admin.system.domain.dto.TeacherDTO;
import com.huitong.admin.system.domain.dto.UserPageDTO;
import com.huitong.admin.system.domain.po.UserPO;
import com.huitong.admin.system.domain.vo.UserVO;
import com.huitong.admin.system.mapper.UserMapper;
import com.huitong.admin.system.service.UserService;
import com.huitong.common.core.constant.UserConstant;
import com.huitong.common.core.domain.Result;
import com.huitong.common.mybatis.domain.PageResult;
import com.huitong.common.mybatis.utils.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户信息服务层
 *
 * @author fromdrowning
 * @date 2024/5/10 17:40
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl implements UserService {

    /**
     * 用户信息数据访问层
     */
    private final UserMapper userMapper;

    /**
     * 新增学生信息
     *
     * @param studentDTO 学生信息
     */
    @Transactional
    @Override
    public void insertStudent(StudentDTO studentDTO) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(studentDTO, userPO);
        userPO.setPassword(BCrypt.hashpw(UserConstant.DEFAULT_PASSWORD));
        userPO.setRole(UserConstant.STUDENT);
        userMapper.insertUser(userPO);
        userMapper.insertUserInfo(userPO);
    }

    /**
     * 新增教师信息
     *
     * @param teacherDTO 教师信息
     */
    @Transactional
    @Override
    public void insertTeacher(TeacherDTO teacherDTO) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(teacherDTO, userPO);
        userPO.setPassword(BCrypt.hashpw(UserConstant.DEFAULT_PASSWORD));
        userPO.setRole(UserConstant.TEACHER);
        userMapper.insertUser(userPO);
        userMapper.insertUserInfo(userPO);
    }

    /**
     * 查询用户信息
     *
     * @param id   学号
     * @param role 角色
     * @return 用户信息
     */
    @Override
    public Result<UserVO> selectUser(Long id, String role) {
        return Result.success(userMapper.selectUser(id, role));
    }

    /**
     * 查询用户信息
     *
     * @param userPageDTO 分页信息
     * @return 分页信息
     */
    @Override
    public PageResult<UserVO> pageUser(UserPageDTO userPageDTO) {
        PageUtils.startPage(userPageDTO, userPageDTO.getRole());
        return PageResult.success(userMapper.pageUser(userPageDTO));
    }

    /**
     * 修改学生信息
     *
     * @param studentDTO 学生信息
     */
    @Override
    public void updateStudent(StudentDTO studentDTO) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(studentDTO, userPO);
        userPO.setRole(UserConstant.STUDENT);
        userMapper.updateUser(userPO);
    }

    /**
     * 修改教师信息
     *
     * @param teacherDTO 教师信息
     */
    @Override
    public void updateTeacher(TeacherDTO teacherDTO) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(teacherDTO, userPO);
        userPO.setRole(UserConstant.TEACHER);
        userMapper.updateUser(userPO);
    }

    /**
     * 删除用户信息
     *
     * @param id   学号
     * @param role 角色
     */
    @Transactional
    @Override
    public void deleteUser(Long id, String role) {
        userMapper.insertUserDeleted(id, role);
        userMapper.insertUserInfoDeleted(id, role);
        userMapper.deleteUser(id, role);
        userMapper.deleteUserInfo(id, role);
        // TODO 删除用户信息时 还需要删除与用户相关的信息
    }
}
